package com.hue.gulimail.gulimailmember.controller;

import java.util.Arrays;
import java.util.Map;

import com.hue.common.utils.Error;
import com.hue.gulimail.gulimailmember.exception.EmailExistException;
import com.hue.gulimail.gulimailmember.exception.UsernameExistException;
import com.hue.gulimail.gulimailmember.exception.UsernamePasswordErrorException;
import com.hue.gulimail.gulimailmember.feign.CouponFeignService;
import com.hue.gulimail.gulimailmember.vo.LoginVo;
import com.hue.gulimail.gulimailmember.vo.RegisterVo;
import com.hue.gulimail.gulimailmember.vo.SocialUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.hue.gulimail.gulimailmember.entity.MemberEntity;
import com.hue.gulimail.gulimailmember.service.MemberService;
import com.hue.common.utils.PageUtils;
import com.hue.common.utils.R;


/**
 * 会员
 *
 * @author yaofengqiao
 * @email 451737523@qq.com
 * @date 2021-08-12 17:07:52
 */
@RestController
@RequestMapping("gulimailmember/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Autowired
    private CouponFeignService couponFeignService;

    /**
     * 测试请求
     */
    @RequestMapping("/test")
    public R list() {
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setNickname("张三");
        R r = couponFeignService.memberCoupon();
        return R.ok().put("member", memberEntity).put("coupon", r.get("coupon"));
    }

    /**
     * 用户登录
     * @param loginVo
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody LoginVo loginVo) {
        try {
            MemberEntity member = memberService.login(loginVo);
            return R.ok().put("data", member);
        } catch (UsernamePasswordErrorException exception) {
            return R.error(Error.USERNAME_PASSWORD_ERROR.getCode(), Error.USERNAME_PASSWORD_ERROR.getMsg());
        }
    }

    /**
     * 社交登录
     * @param socialUserVo
     * @return
     */
    @PostMapping("social/login")
    public R socialLogin(@RequestBody SocialUserVo socialUserVo){
        try {
            MemberEntity member = memberService.login(socialUserVo);
            return R.ok().put("data", member);
        } catch (UsernamePasswordErrorException exception) {
            return R.error(Error.USERNAME_PASSWORD_ERROR.getCode(), Error.USERNAME_PASSWORD_ERROR.getMsg());
        }
    }

    /**
     * 注册账号
     *
     * @param registerVo
     * @return
     */
    @PostMapping("/register")
    public R register(@RequestBody RegisterVo registerVo) {
        try {
            memberService.register(registerVo);
        } catch (UsernameExistException usernameExistException) {
            return R.error(Error.USERNAME_EXIST_ERROR.getCode(), Error.USERNAME_EXIST_ERROR.getMsg());
        } catch (EmailExistException emailExistException) {
            return R.error(Error.EMAIL_EXIST_ERROR.getCode(), Error.EMAIL_EXIST_ERROR.getMsg());
        }
        return R.ok("注册成功");
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("gulimailmember:member:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 根据用户id获取用户信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("gulimailmember:member:info")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("gulimailmember:member:save")
    public R save(@RequestBody MemberEntity member) {
        memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("gulimailmember:member:update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("gulimailmember:member:delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
